/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TinTuc;

import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.*;

/**
 *
 * @author Admin
 */
public class XPathReader {

    protected String domain;
    protected String rootroot;
    protected String broot;
    protected String aroot;
    private String img;
    private String date;
    private String summary;
    private XMLOutputter printer = null;
    private File xmlFile;

    public XPathReader() {
        this.printer = new XMLOutputter();
    }

    public XPathReader(File xmlFile) {

        this.printer = new XMLOutputter();
        this.xmlFile = xmlFile;
    }
//<editor-fold desc="Get Element">

    public Element getDomainElement(Document doc, String domainname) {

        SAXBuilder parser = new SAXBuilder();
        try {
            Element Root = doc.getRootElement();
            List<Element> lst = Root.getChildren("domain");
            for (int i = 0; i < lst.size(); i++) {
                Element e = lst.get(i);
                if (e.getAttributeValue("id").equals(domainname)) {
                    return e;
                }
            }
        } catch (Exception e) {
        }
        return null;
    }

    public Element getRootRootElement(Element domain) {
        try {
            return domain.getChild("rootroot");
        } catch (NullPointerException ex) {
            return null;
        }
    }

    public Element getBRootElement(Element domain) {
        try {
            return domain.getChild("broot");
        } catch (Exception e) {
            return null;
        }

    }

    public Element getARootElement(Element domain) {
        try {
            return domain.getChild("aroot");
        } catch (Exception e) {
            return null;
        }

    }

    public Element getLinkElement(Element domain) {
        try {
            return domain.getChild("link");
        } catch (Exception e) {
            return null;
        }

    }

    public Element getTitleElement(Element domain) {
        try {
            return domain.getChild("title");
        } catch (Exception e) {
            return null;
        }

    }

    public Element getImgElement(Element domain) {
        try {
            return domain.getChild("img");
        } catch (Exception e) {
            return null;
        }

    }

    public Element getDateElement(Element domain) {
        try {
            return domain.getChild("date");
        } catch (Exception e) {
            return null;
        }

    }

    public Element getSummaryElement(Element domain) {
        try {
            return domain.getChild("summary");
        } catch (Exception e) {
            return null;
        }

    }
    //</editor-fold>
//<editor-fold desc="getter setter">

    public String getRootroot(Element eDomain) {
        String rr = "";
        try {
            rr = eDomain.getChild("rootroot").getText();

        } catch (Exception e) {
            System.out.println("Loi");
        }
        return rr;
    }

    public void setRootroot(String rootroot) {
        this.rootroot = rootroot;
    }

    public String getBRoot(Element eDomain) {
        String rr = "";
        try {
            rr = eDomain.getChild("broot").getText();

        } catch (Exception e) {
            System.out.println("Loi");
        }
        return rr;
    }

    public void setBRoot(String broot) {
        this.broot = broot;
    }
    protected String link;

    public String getLink(Element eDomain) {
        String rr = "";
        try {
            rr = eDomain.getChild("link").getText();

        } catch (Exception e) {
            System.out.println("Loi");
        }
        return rr;
    }

    public void setLink(String link) {
        this.link = link;
    }
    protected String title;

    public String getARoot(Element eDomain) {
        String rr = "";
        try {
            rr = eDomain.getChild("aroot").getText();

        } catch (Exception e) {
            System.out.println("Loi");
        }
        return rr;
    }

    public void setAroot(String aroot) {
        this.aroot = aroot;
    }

    public String getTitle(Element eDomain) {
        String rr = "";
        try {
            rr = eDomain.getChild("title").getText();

        } catch (Exception e) {
            System.out.println("Loi");
        }
        return rr;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Element getDomain(String domainname) {
        if (!xmlFile.exists()) {
            return null;
        }
        SAXBuilder parser = new SAXBuilder();
        try {
            Document doc = parser.build(xmlFile);
            Element Root = (Element) doc.getRootElement();
            List<Element> lst = Root.getChildren("domain");
            for (int i = 0; i < lst.size(); i++) {
                Element e = (Element) lst.get(i);
                if (e.getAttributeValue("id").equals(domainname)) {
                    return e;
                }
            }
        } catch (JDOMException | IOException e) {
        }
        return null;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    /**
     * @return the img
     */
    public String getImg(Element eDomain) {
        String rr = "";
        try {
            rr = eDomain.getChild("img").getText();

        } catch (Exception e) {
            System.out.println("Loi");
        }
        return rr;
    }

    /**
     * @param img the img to set
     */
    public void setImg(String img) {
        this.img = img;
    }

    /**
     * @return the date
     */
    public String getDate(Element eDomain) {
        String rr = "";
        try {
            rr = eDomain.getChild("date").getText();

        } catch (Exception e) {
            System.out.println("Loi");
        }
        return rr;
    }

    /**
     * @param date the date to set
     */
    public void setDate(String date) {
        this.date = date;
    }

    /**
     * @return the summary
     */
    public String getSummary(Element eDomain) {
        String rr = "";
        try {
            rr = eDomain.getChild("summary").getText();

        } catch (Exception e) {
            System.out.println("Loi");
        }
        return rr;
    }

    /**
     * @param summary the summary to set
     */
    public void setSummary(String summary) {
        this.summary = summary;
    }
//</editor-fold>
//<editor-fold desc="Test">
//    public static void main(String[] args) throws JDOMException, IOException {
//        File fi = new File("cautrucxpathcuatrangweb.xml");
//        XPathWriter xmlW = new XPathWriter(fi);
//        //xmlW.UpdateDomain("def","1111111111111111111");
//        System.out.println(xmlW.UpdateRootRoot("abc", "1"));
//        System.out.println(xmlW.UpdateARoot("abc", "2"));
//        System.out.println(xmlW.UpdateBRoot("abc", "3"));
//        System.out.println(xmlW.UpdateLink("abc", "4"));
//        System.out.println(xmlW.UpdateTitle("abc", "5"));
//        System.out.println(xmlW.UpdateImg("abc", "6"));
//        System.out.println(xmlW.UpdateDate("abc", "7"));
//        System.out.println(xmlW.UpdateSummary("abc", "8"));
//
//    }
    //</editor-fold>
}
